﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Collections;
using DataRecv.db;
using DataRecv.enums;

namespace DataRecv.models
{
    /// <summary>
    /// 系统参数类
    /// </summary>
    public class SystemParam
    {
        /// <summary>
        /// 系统参数表
        /// </summary>
        private Hashtable m_ht;

        #region 卡口参数

        /// <summary>
        /// 卡口图片命名模式
        /// </summary>
        public Enum_PicNameMode KKPicNameMode;
        /// <summary>
        /// 卡口子文件夹模式
        /// </summary>
        public Enum_SubFolderMode KKSubFolderMode;

        /// <summary>
        /// 卡口启用实时上传
        /// </summary>
        public bool KKUploadEnable;

        /// <summary>
        /// 卡口启用失败重传
        /// </summary>
        public bool KKFailReTryEnable;

        /// <summary>
        /// 卡口图片中心PicCenter的IP
        /// </summary>
        public string KKUploadIP;

        /// <summary>
        /// 卡口图片中心PicCenter的port
        /// </summary>
        public string KKUploadPort;

        /// <summary>
        /// 卡口图片保存天数
        /// </summary>
        public int KKSaveDays;

        /// <summary>
        /// 卡口图片带尾部信息
        /// </summary>
        public bool KKTailEnable;

        /// <summary>
        /// 卡口子文件夹自定义存储方式
        /// </summary>
        public string KKDiy;

        /// <summary>
        /// 卡口生成车牌小图
        /// </summary>
        public bool KKPlateEnable;

        /// <summary>
        /// 卡口图片命名分隔符
        /// </summary>
        public string KKNameSplitor;

        /// <summary>
        /// 卡口图片自定义命名规则
        /// </summary>
        public string KKNameDiy;

        /// <summary>
        /// 卡口推送启用
        /// </summary>
        public bool KKPushEnable;

        /// <summary>
        /// 卡口失败重推
        /// </summary>
        public bool KKRePushEnable;

        #endregion

        #region 电警参数

        /// <summary>
        /// 电警图片命名模式
        /// </summary>
        public Enum_PicNameMode DJPicNameMode;

        /// <summary>
        /// 电警子文件夹模式
        /// </summary>
        public Enum_SubFolderMode DJSubFolderMode;

        /// <summary>
        /// 电警启用实时上传
        /// </summary>
        public bool DJUploadEnable;

        /// <summary>
        /// 电警启用失败重传
        /// </summary>
        public bool DJFailReTryEnable;

        /// <summary>
        /// 电警图片完整性校验（必须校验，母万国修该，用作超速10过滤开关）
        /// </summary>
        public bool DJPicCheck;

        /// <summary>
        /// 电警启用压缩
        /// </summary>
        public bool DJCompressEnable;

        /// <summary>
        /// 压缩值，单位KB
        /// </summary>
        public string DJCompressValue;

        /// <summary>
        /// 电警图片中心PicCenter的IP
        /// </summary>
        public string DJUploadIP;

        /// <summary>
        /// 电警图片中心PicCenter的port
        /// </summary>
        public string DJUploadPort;

        /// <summary>
        /// 电警图片保存天数
        /// </summary>
        public int DJSaveDays;

        /// <summary>
        /// 电警启用车牌过滤
        /// </summary>
        public bool DJPlateFilterEnable;

        /// <summary>
        /// 电警车牌过滤类型
        /// </summary>
        public Enum_PlateFilterType DJPlateFilterType;

        /// <summary>
        /// 电警图片带尾部信息
        /// </summary>
        public bool DJTailEnable;

        /// <summary>
        /// 电警子文件夹自定义存储方式
        /// </summary>
        public string DJDiy;

        /// <summary>
        /// 电警生成车牌小图
        /// </summary>
        public bool DJPlateEnable;

        /// <summary>
        /// 电警图片命名分隔符
        /// </summary>
        public string DJNameSplitor;


        /// <summary>
        /// 电警图片自定义命名规则
        /// </summary>
        public string DJNameDiy;

        /// <summary>
        /// 电警推送启用
        /// </summary>
        public bool DJPushEnable;

        /// <summary>
        /// 电警失败重推
        /// </summary>
        public bool DJRePushEnable;

        #endregion

        /// <summary>
        /// 抓拍参数
        /// </summary>
        public Hashtable SnapParams;

        /// <summary>
        /// 违法行为代码
        /// </summary>
        public Hashtable Wfxwdms;



        #region 图片合成

        /// <summary>
        /// 两张图片合成方式
        /// </summary>
        public Enum_PicJoinMode TwoPicsJoinMode;
        /// <summary>
        /// 三张图片合成方式
        /// </summary>
        public Enum_PicJoinMode ThreePicsJoinMode;
        /// <summary>
        /// 四张图片合成方式
        /// </summary>
        public Enum_PicJoinMode FourPicsJoinMode;

        #endregion

        #region 字符叠加参数

        /// <summary>
        /// 启用字符叠加
        /// </summary>
        public bool PrintEnable;

        /// <summary>
        /// 叠加时间
        /// </summary>
        public bool PrintTime;

        /// <summary>
        /// 叠加路口名称
        /// </summary>
        public bool PrintRoad;

        /// <summary>
        /// 叠加方向
        /// </summary>
        public bool PrintDirection;

        /// <summary>
        /// 叠加车道
        /// </summary>
        public bool PrintCD;

        /// <summary>
        /// 叠加抓拍系统
        /// </summary>
        public bool PrintCaptureSystem;

        /// <summary>
        /// 叠加车牌颜色
        /// </summary>
        public bool PrintPlateColor;

        /// <summary>
        /// 叠加号牌号码
        /// </summary>
        public bool PrintHPHM;

        /// <summary>
        /// 叠加设备编号
        /// </summary>
        public bool PrintSBBH;

        /// <summary>
        /// 叠加防伪码
        /// </summary>
        public bool PrintFWM;

        /// <summary>
        /// 叠加位置
        /// </summary>
        public Enum_PrintPosition PrintPosition;
        #endregion

        #region 其他参数

        /// <summary>
        /// 本机IP
        /// </summary>
        public string LocalIP;

        /// <summary>
        /// 启用硬件看门狗
        /// </summary>
        public bool DogEnable;

        /// <summary>
        /// 看门狗串口号，如COM1
        /// </summary>
        public string DogPort;

        /// <summary>
        /// 允许看门狗关机
        /// </summary>
        public bool DogEnableShutDown;

        /// <summary>
        /// 看门狗关机时
        /// </summary>
        public string DogShutDownHour;

        /// <summary>
        /// 看门狗关机分
        /// </summary>
        public string DogShutDownMin;

        /// <summary>
        /// 看门狗断电间隔（分）
        /// </summary>
        public string DogShutDownSpan;

        /// <summary>
        /// 启用软件每日定时关闭，清理内存
        /// </summary>
        public bool DailyCloseEnable;

        /// <summary>
        /// 软件关闭时
        /// </summary>
        public string DailyCloseHour;

        /// <summary>
        /// 软件关闭分
        /// </summary>
        public string DailyCloseMin;

        /// <summary>
        /// 参数同步服务器IP
        /// </summary>
        public string SynServerIP;

        /// <summary>
        /// 参数同步服务器Port
        /// </summary>
        public string SynServerPort;

        /// <summary>
        /// 调试日志开关
        /// </summary>
        public bool DebugLogEnable;

        /// <summary>
        /// 业务日志开关
        /// </summary>
        public bool WorkLogEnable;

        /// <summary>
        /// webf发布类型
        /// </summary>
        public Enum_WebPathType WebPathType;

        /// <summary>
        /// MQURI
        /// </summary>
        public string MqUri;

        /// <summary>
        /// MQTOPIC
        /// </summary>
        public string MqTopic;

        /// <summary>
        /// 推送监听端口
        /// </summary>
        public string PushPort;

        /// <summary>
        /// 推送模式
        /// </summary>
        public JRCore.enums.Enum_TcpWorkMode PushWorkMode;

        /// <summary>
        /// 推送超时时间
        /// </summary>
        public string PushTimeOut;

        #endregion

        /// <summary>
        /// BRT
        /// </summary>
        public bool BRTEnable;

        /// <summary>
        /// 构造函数，生成所有系统参数
        /// </summary>
        public SystemParam()
        {
            try
            {
                m_ht = DBHelper.GetInstance().SelectSystemParam();
                if (m_ht != null)
                {
                    string str = "";
                    //卡口参数
                    LocalIP = Read(Enum_SystemParamName.LocalIP);
                    str = Read(Enum_SystemParamName.KKPicNameMode);
                    if (str != "") KKPicNameMode = (Enum_PicNameMode)Enum.Parse(typeof(Enum_PicNameMode), str);
                    str = Read(Enum_SystemParamName.KKSubFolderMode);
                    if (str != "") KKSubFolderMode = (Enum_SubFolderMode)Enum.Parse(typeof(Enum_SubFolderMode), str);
                    str = Read(Enum_SystemParamName.KKUploadEnable);
                    if (str != "") KKUploadEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.KKFailReTryEnable);
                    if (str != "") KKFailReTryEnable = bool.Parse(str);
                    KKUploadIP = Read(Enum_SystemParamName.KKUploadIP);
                    KKUploadPort = Read(Enum_SystemParamName.KKUploadPort);
                    str = Read(Enum_SystemParamName.KKSaveDays);
                    if (str != "") KKSaveDays = int.Parse(str);
                    str = Read(Enum_SystemParamName.KKTailEnable);
                    if (str != "") KKTailEnable = bool.Parse(str);
                    KKDiy = Read(Enum_SystemParamName.KKDiy);
                    str = Read(Enum_SystemParamName.KKPlateEnable);
                    if (str != "") KKPlateEnable = bool.Parse(str);
                    KKNameSplitor = Read(Enum_SystemParamName.KKNameSplitor);
                    KKNameDiy = Read(Enum_SystemParamName.KKNameDiy);
                    str = Read(Enum_SystemParamName.KKPushEnable);
                    KKPushEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.KKRePushEnable);
                    KKRePushEnable = bool.Parse(str);

                    //电警参数
                    str = Read(Enum_SystemParamName.DJPicNameMode);
                    if (str != "") DJPicNameMode = (Enum_PicNameMode)Enum.Parse(typeof(Enum_PicNameMode), str);
                    str = Read(Enum_SystemParamName.DJSubFolderMode);
                    if (str != "") DJSubFolderMode = (Enum_SubFolderMode)Enum.Parse(typeof(Enum_SubFolderMode), str);
                    str = Read(Enum_SystemParamName.DJUploadEnable);
                    if (str != "") DJUploadEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.DJFailReTryEnable);
                    if (str != "") DJFailReTryEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.DJPicCheck);
                    if (str != "") DJPicCheck = bool.Parse(str);
                    str = Read(Enum_SystemParamName.DJCompressEnable);
                    if (str != "") DJCompressEnable = bool.Parse(str);
                    DJCompressValue = Read(Enum_SystemParamName.DJCompressValue);
                    DJUploadIP = Read(Enum_SystemParamName.DJUploadIP);
                    DJUploadPort = Read(Enum_SystemParamName.DJUploadPort);
                    str = Read(Enum_SystemParamName.DJSaveDays);
                    if (str != "") DJSaveDays = int.Parse(str);
                    str = Read(Enum_SystemParamName.DJPlateFilterEnable);
                    if (str != "") DJPlateFilterEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.DJPlateFilterType);
                    if (str != "") DJPlateFilterType = (Enum_PlateFilterType)Enum.Parse(typeof(Enum_PlateFilterType), str);
                    str = Read(Enum_SystemParamName.DJTailEnable);
                    if (str != "") DJTailEnable = bool.Parse(str);
                    DJDiy = Read(Enum_SystemParamName.DJDiy);
                    str = Read(Enum_SystemParamName.DJPlateEnable);
                    if (str != "") DJPlateEnable = bool.Parse(str);
                    DJNameSplitor = Read(Enum_SystemParamName.DJNameSplitor);
                    DJNameDiy = Read(Enum_SystemParamName.DJNameDiy);
                    str = Read(Enum_SystemParamName.DJPushEnable);
                    DJPushEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.DJRePushEnable);
                    DJRePushEnable = bool.Parse(str);

                    //图片合成参数
                    str = Read(Enum_SystemParamName.TwoPicsJoinMode);
                    if (str != "") TwoPicsJoinMode = (Enum_PicJoinMode)Enum.Parse(typeof(Enum_PicJoinMode), str);
                    str = Read(Enum_SystemParamName.ThreePicsJoinMode);
                    if (str != "") ThreePicsJoinMode = (Enum_PicJoinMode)Enum.Parse(typeof(Enum_PicJoinMode), str);
                    str = Read(Enum_SystemParamName.FourPicsJoinMode);
                    if (str != "") FourPicsJoinMode = (Enum_PicJoinMode)Enum.Parse(typeof(Enum_PicJoinMode), str);

                    //其他参数
                    str = Read(Enum_SystemParamName.DogEnable);
                    if (str != "") DogEnable = bool.Parse(str);
                    DogPort = Read(Enum_SystemParamName.DogPort);
                    str = Read(Enum_SystemParamName.DogEnableShutDown);
                    if (str != "") DogEnableShutDown = bool.Parse(str);
                    DogShutDownHour = Read(Enum_SystemParamName.DogShutDownHour);
                    DogShutDownMin = Read(Enum_SystemParamName.DogShutDownMin);
                    DogShutDownSpan = Read(Enum_SystemParamName.DogShutDownSpan);
                    str = Read(Enum_SystemParamName.DailyCloseEnable);
                    if (str != "") DailyCloseEnable = bool.Parse(str);
                    DailyCloseHour = Read(Enum_SystemParamName.DailyCloseHour);
                    DailyCloseMin = Read(Enum_SystemParamName.DailyCloseMin);
                    SynServerIP = Read(Enum_SystemParamName.SynServerIP);
                    SynServerPort = Read(Enum_SystemParamName.SynServerPort);
                    str = Read(Enum_SystemParamName.DebugLogEnable);
                    if (str != "") DebugLogEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.WorkLogEnable);
                    if (str != "") WorkLogEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.WebPathType);
                    if (str != "") WebPathType = (Enum_WebPathType)Enum.Parse(typeof(Enum_WebPathType), str);
                    MqUri = Read(Enum_SystemParamName.MqUri);
                    MqTopic = Read(Enum_SystemParamName.MqTopic);
                    PushPort = Read(Enum_SystemParamName.PushPort);
                    str = Read(Enum_SystemParamName.PushWorkMode);
                    if (str != "") PushWorkMode = (JRCore.enums.Enum_TcpWorkMode)Enum.Parse(typeof(JRCore.enums.Enum_TcpWorkMode), str);
                    PushTimeOut = Read(Enum_SystemParamName.PushTimeOut);
                    if (PushTimeOut == "") PushTimeOut = "0";

                    //抓拍参数
                    SnapParams = new Hashtable();
                    Hashtable ht = SnapParam.GetModels();
                    foreach (Enum_CaptureSystem s in Enum.GetValues(typeof(Enum_CaptureSystem)))
                    {
                        if (s != Enum_CaptureSystem.NONE)
                        {
                            SnapParam sp = new SnapParam();
                            sp.snaptype = s;
                            if ((ht != null) && (ht.Contains(s)))
                            {
                                sp = (SnapParam)ht[s];
                            }
                            SnapParams.Add(s, sp);
                        }
                    }
                    ht.Clear();

                    //违法行为代码
                    Wfxwdms = new Hashtable();
                    ht = VioParam.GetModels();
                    foreach (Enum_VioType s in Enum.GetValues(typeof(Enum_VioType)))
                    {
                        VioParam wfdm = new VioParam();
                        wfdm.viotype = s;
                        if ((ht != null) && (ht.Contains(s)))
                        {
                            wfdm = (VioParam)ht[s];
                        }
                        Wfxwdms.Add(s, wfdm);
                    }
                    ht.Clear();

                    //字符叠加
                    str = Read(Enum_SystemParamName.PrintEnable);
                    if (str != "") PrintEnable = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintTime);
                    if (str != "") PrintTime = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintRoad);
                    if (str != "") PrintRoad = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintDirection);
                    if (str != "") PrintDirection = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintCD);
                    if (str != "") PrintCD = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintCaptureSystem);
                    if (str != "") PrintCaptureSystem = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintPlateColor);
                    if (str != "") PrintPlateColor = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintHPHM);
                    if (str != "") PrintHPHM = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintSBBH);
                    if (str != "") PrintSBBH = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintFWM);
                    if (str != "") PrintFWM = bool.Parse(str);
                    str = Read(Enum_SystemParamName.PrintPosition);
                    if (str != "") PrintPosition = (Enum_PrintPosition)Enum.Parse(typeof(Enum_PrintPosition), str);

                    //BRT
                    str = Read(Enum_SystemParamName.BRTEnable);
                    if (str != "") BRTEnable = bool.Parse(str);
                }
            }
            catch { }
        }

        /// <summary>
        /// 从HashTable读取一个参数
        /// </summary>
        /// <param name="paramname"></param>
        /// <returns></returns>
        public string Read(Enum_SystemParamName paramname)
        {
            string ans = "";
            try
            {
                if (m_ht != null)
                {
                    if (m_ht.Contains(paramname.ToString()))
                    {
                        ans = m_ht[paramname.ToString()].ToString();
                    }
                    else
                    {
                        ans = "";
                    }
                }
            }
            catch { ans = ""; }
            return ans;
        }

        /// <summary>
        /// 保存参数到数据库
        /// </summary>
        /// <param name="paramname">参数名</param>
        /// <param name="paramvalue">参数值</param>
        /// <returns>保存结果</returns>
        public bool Save(Enum_SystemParamName paramname, object paramvalue)
        {
            bool ans = false;
            try
            {
                if (paramvalue != null)
                {
                    ans = DBHelper.GetInstance().EditSystemParam(paramname.ToString(), paramvalue.ToString());
                }
            }
            catch { ans = false; }
            return ans;
        }

    }
}
